package com.pheruagor.handmanager.view {
    import com.pheruagor.handmanager.controller.CloseClubCreationPopUpCommand;
    import com.pheruagor.handmanager.model.business.Club;
    import com.pheruagor.handmanager.view.component.ClubCreationPopUp;

    import flash.events.MouseEvent;

    import mx.controls.Alert;
    import mx.events.FlexEvent;
    import mx.utils.UIDUtil;

    import org.puremvc.as3.interfaces.INotification;
    import org.puremvc.as3.patterns.mediator.Mediator;

    public class ClubCreationPopUpMediator extends Mediator {
        public static const NAME:String = "clubCreationPopUpMediator";
        public static const CANCEL:String = NAME + "Cancel";

        //-----------------------------------------------------
        // Properties
        //-----------------------------------------------------

        //-----------------------------------------------------
        // Initialization
        //-----------------------------------------------------
        public function ClubCreationPopUpMediator(view:ClubCreationPopUp) {
            super(NAME, view);
        }

        //-----------------------------------------------------
        // Access
        //-----------------------------------------------------
        public function get clubCreationPopUp():ClubCreationPopUp {
            return ClubCreationPopUp(viewComponent);
        }

        //-----------------------------------------------------
        // IMediator Interface
        //-----------------------------------------------------
        override public function onRegister():void {
            clubCreationPopUp.addEventListener(FlexEvent.CREATION_COMPLETE, onViewCreationCompleted)
        }

        override public function onRemove():void {
            clubCreationPopUp.removeEventListener(FlexEvent.CREATION_COMPLETE, onViewCreationCompleted)
            clubCreationPopUp.cancelButton.removeEventListener(MouseEvent.CLICK, onCancelButtonClicked);
            clubCreationPopUp.okButton.removeEventListener(MouseEvent.CLICK, onOkButtonClicked);
        }

        //-----------------------------------------------------
        // Event handler
        //-----------------------------------------------------
        private function onViewCreationCompleted(event:FlexEvent):void {
            clubCreationPopUp.cancelButton.addEventListener(MouseEvent.CLICK, onCancelButtonClicked);
            clubCreationPopUp.okButton.addEventListener(MouseEvent.CLICK, onOkButtonClicked);
        }

        private function onOkButtonClicked(event:MouseEvent):void {
            if (clubCreationPopUp.clubNameTextImput.text.length == 0) {
                Alert.show("Vous devez définir un nom pour le club.", "Information");
                return;
            }
            var club:Club = new Club();
            club.id = UIDUtil.createUID();
            club.name = clubCreationPopUp.clubNameTextImput.text;
            sendNotification(CloseClubCreationPopUpCommand.NAME, {"popup": clubCreationPopUp, "club": club});
        }

        private function onCancelButtonClicked(event:MouseEvent):void {
            sendNotification(CloseClubCreationPopUpCommand.NAME, {"popup": clubCreationPopUp, "club": null});
        }
        //-----------------------------------------------------
        // Operation
        //-----------------------------------------------------
    }
}